{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "| **方法**             | **优点**                                                     | **缺点**                                                     | **适用场景**                                                 |\n",
    "| -------------------- | ------------------------------------------------------------ | ------------------------------------------------------------ | ------------------------------------------------------------ |\n",
    "| **虚函数**           | 动态分派效率较高，支持复杂的运行时多态。                     | 开销比静态分派高，继承层次多时复杂性上升。                   | 面向对象模型，运行时多态需求明确的场景。                     |\n",
    "| **组合 + 虚函数**    | 灵活性更强，代码复用性高，避免多重继承问题。                 | 额外组合调用开销，性能略逊于直接虚函数调用。                 | 需要灵活扩展或组合复用的场景。                               |\n",
    "| **模板（静态分派）** | 性能最佳，完全消除动态分派开销，编译时优化充分。             | 代码膨胀，类型固定，无法实现运行时灵活多态。                 | 高性能需求场景，如数值计算、嵌入式开发。                     |\n",
    "| **`std::function`**    | 提供类型安全的多态封装，灵活性最高，适合回调或动态行为需求。 | 性能最差，包含封装和动态分派的额外开销。                     | 灵活性要求最高的场景，如事件驱动架构或回调函数管理。         |\n",
    "| **CRTP**             | 性能接近模板（静态分派），支持代码复用，允许在基类中实现通用逻辑。 | 类型固定，无法支持运行时多态；代码复杂度较高，模板错误可能难以理解。 | 高性能需求场景，同时需要代码复用的场景，如编译期优化、多态替代方案。 |"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "vscode": {
     "languageId": "shellscript"
    }
   },
   "outputs": [],
   "source": [
    "! g++ -std=c++20 -O2 benchmark_test.cpp -lbenchmark -lpthread -o benchmark_test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "vscode": {
     "languageId": "shellscript"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2025-10-04T21:39:23+08:00\n",
      "Running ./benchmark_test\n",
      "Run on (16 X 3792.93 MHz CPU s)\n",
      "CPU Caches:\n",
      "  L1 Data 32 KiB (x8)\n",
      "  L1 Instruction 32 KiB (x8)\n",
      "  L2 Unified 1024 KiB (x8)\n",
      "  L3 Unified 16384 KiB (x1)\n",
      "Load Average: 0.25, 0.24, 0.26\n",
      "--------------------------------------------------------------------------------------------------------\n",
      "Benchmark                                              Time             CPU   Iterations UserCounters...\n",
      "--------------------------------------------------------------------------------------------------------\n",
      "BM_VirtualFunction/10000000_mean                27331271 ns     27326221 ns            5 items_per_second=367.787M/s\n",
      "BM_VirtualFunction/10000000_median              26933184 ns     26933421 ns            5 items_per_second=371.286M/s\n",
      "BM_VirtualFunction/10000000_stddev               2130940 ns      2132533 ns            5 items_per_second=29.5126M/s\n",
      "BM_VirtualFunction/10000000_cv                      7.80 %          7.80 %             5 items_per_second=8.02%\n",
      "BM_CompositionVirtualFunction/10000000_mean     28171094 ns     28170131 ns            5 items_per_second=355.089M/s\n",
      "BM_CompositionVirtualFunction/10000000_median   27966257 ns     27963443 ns            5 items_per_second=357.61M/s\n",
      "BM_CompositionVirtualFunction/10000000_stddev     536534 ns       536348 ns            5 items_per_second=6.7348M/s\n",
      "BM_CompositionVirtualFunction/10000000_cv           1.90 %          1.90 %             5 items_per_second=1.90%\n",
      "BM_TemplateStaticDispatch/10000000_mean         21778138 ns     21778184 ns            5 items_per_second=459.183M/s\n",
      "BM_TemplateStaticDispatch/10000000_median       21760286 ns     21760425 ns            5 items_per_second=459.55M/s\n",
      "BM_TemplateStaticDispatch/10000000_stddev         102387 ns       102432 ns            5 items_per_second=2.15184M/s\n",
      "BM_TemplateStaticDispatch/10000000_cv               0.47 %          0.47 %             5 items_per_second=0.47%\n",
      "BM_StdFunction/10000000_mean                    25520008 ns     25519019 ns            5 items_per_second=392.239M/s\n",
      "BM_StdFunction/10000000_median                  25502756 ns     25501653 ns            5 items_per_second=392.131M/s\n",
      "BM_StdFunction/10000000_stddev                    879008 ns       878477 ns            5 items_per_second=13.6132M/s\n",
      "BM_StdFunction/10000000_cv                          3.44 %          3.44 %             5 items_per_second=3.47%\n",
      "BM_CRTP/10000000_mean                           21786986 ns     21786141 ns            5 items_per_second=459.018M/s\n",
      "BM_CRTP/10000000_median                         21828953 ns     21828003 ns            5 items_per_second=458.127M/s\n",
      "BM_CRTP/10000000_stddev                           117024 ns       117258 ns            5 items_per_second=2.47586M/s\n",
      "BM_CRTP/10000000_cv                                 0.54 %          0.54 %             5 items_per_second=0.54%\n"
     ]
    }
   ],
   "source": [
    "! ./benchmark_test --benchmark_repetitions=5 --benchmark_report_aggregates_only=true"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "C++17",
   "language": "C++17",
   "name": "xcpp17"
  },
  "language_info": {
   "codemirror_mode": "text/x-c++src",
   "file_extension": ".cpp",
   "mimetype": "text/x-c++src",
   "name": "c++",
   "version": "17"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
